package com.dyh.quickdemozwei;

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.FiberForkJoinScheduler;
import co.paralleluniverse.fibers.FiberScheduler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
public class TestFiberCorn {

  @AllArgsConstructor
  class Job{
      int period;//秒
      Runnable runnable;
  }

  private FiberScheduler scheduler =
            new FiberForkJoinScheduler("my-FiberScheduler", 1, null, false);


  @Test public void test4Sleep() throws ExecutionException, InterruptedException {
      Fiber<Void> fiber = new Fiber<>("f114514",scheduler,()->{
          for (;;){
              Fiber.sleep(8*1000L);
              log.info("code after fiber  sleep");
          }

      });

      fiber.start();

      fiber.get();
  }

  class MyFiberScheduleThread extends Thread{
      Job[] jobs;
      List<Fiber<Void>> fibers;

      public MyFiberScheduleThread(Job... jobs) {
          this.jobs = jobs;
      }

      @Override
      public void run() {
          fibers=new ArrayList<>();
          for (Job job : jobs) {

              Fiber<Void> fiber = new Fiber<>(scheduler,()->{
                  for (;;){
                      Fiber.sleep(job.period*1000L);
                      job.runnable.run();

                      //先run再睡似乎会出现重叠的 猜测 j1 执行后没睡 切到j2 又没睡切到j3
//                      job.runnable.run();
//                      Fiber.sleep(job.period*1000L);

                  }

              });

              fibers.add(fiber);
          }

          for (Fiber<Void> fiber : this.fibers) {
              fiber.start();
          }

          /*让线程卡住 下面语句应该永远执行不完*/
          try {
              fibers.get(0).join();
          } catch (ExecutionException e) {
              e.printStackTrace();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }

  }

  @Test public void test() throws InterruptedException {

      Runnable r1 = ()->{
          log.info("{}-j1-1执行",Thread.currentThread().getName());
          System.out.println();
      };

      Job j1 =  new Job(1,r1);
      Job j2 = new Job(1,()->log.info("{}-j1-2执行",Thread.currentThread().getName()));

      Job j3 =  new Job(2,()->log.info("{}-j2-1执行",Thread.currentThread().getName()));
      Job j4 =    new Job(2,()->log.info("{}-j2-2执行",Thread.currentThread().getName()));

      Job j5 =  new Job(3,()->log.info("{}-j3-1执行",Thread.currentThread().getName()));

      Job j6=   new Job(4,()->log.info("{}-j4-1执行",Thread.currentThread().getName()));
      Job j7=   new Job(4,()->log.info("{}-j4-2执行",Thread.currentThread().getName()));
      Job j8=    new Job(4,()->log.info("{}-j4-3执行",Thread.currentThread().getName()));



      MyFiberScheduleThread scheduleThread = new MyFiberScheduleThread(

              j1,j2,j3

      );
      scheduleThread.setName("my-scheduleThread");

      scheduleThread.start();

      scheduleThread.join();
  }


}
